package com.payment.ktb.activity.main4;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.AlertInterface;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.activity.ForgetPasswordActivity;
import com.payment.ktb.activity.MainActivity;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.ToastUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/6/4.
 */
public class ModifyPasswordActivity extends BaseActivity {

    String oldPass;
    String newPass;

    @BindView(R.id.et_modifypassword_oldpass)
    EditText et_modifypassword_oldpass;
    @BindView(R.id.et_modifypassword_newpass)
    EditText et_modifypassword_newpass;
    @BindView(R.id.iv_modifypass_newpasscheck)
    ImageView iv_modifypass_newpasscheck;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modifypasswrod);
        ButterKnife.bind(this);
        initTitle("修改密码");
        et_modifypassword_newpass.addTextChangedListener(textWatcher);
    }



    @OnClick({R.id.tv_modifypassword_forget,R.id.btn_modifypassword_cofirm,R.id.iv_modifypass_newpasscheck})
    public void clickEvent(View view) {

        switch (view.getId()) {

            case R.id.tv_modifypassword_forget:
               startActivity(ForgetPasswordActivity.class);
                break;

            //
            case R.id.btn_modifypassword_cofirm:
                initData();
                if (checkPass()) {
                    submit();
                }
                break;

        }

    }

    /**
     *
     */
    private boolean checkPass() {

        if (TextUtils.isEmpty(oldPass) || oldPass.length() < 6) {
            ToastUtils.showToast("原密码至少为6位！");
            return false;
        } else if (TextUtils.isEmpty(newPass) || newPass.length() < 6) {
            ToastUtils.showToast("新密码至少为6位！");
            return false;
        }
        return true;

    }


    /**
     *
     */
    private void initData() {
        oldPass = et_modifypassword_oldpass.getText().toString().trim();
        newPass = et_modifypassword_newpass.getText().toString().trim();
    }


    /**
     *
     */
    private void submit() {

        progressDialogUtils.showProgress();

        //构造传输参数
        HashMap<String, String> map = new HashMap<String,String>();
        map.put("oldPass",oldPass);
        map.put("newPass",newPass);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserLoginPassModifyState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                ToastUtils.showToast("修改密码成功！");
                startActivity(MainActivity.class);
            }
            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }


    private TextWatcher textWatcher = new TextWatcher() {



        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String etValues = s.toString();
            if (etValues.trim().length() == 6){
                iv_modifypass_newpasscheck.setVisibility(View.VISIBLE);
            }else {
                iv_modifypass_newpasscheck.setVisibility(View.GONE);
            }
        }

    };

}
